package util.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import util.enumeration.Interest;
import util.enumeration.Language;
import util.enumhelper.LocalizedInterest;
import util.enumhelper.SupportedLocale;
import util.localization.WebLocalization;

@FacesConverter(value = "interestConverter", forClass = util.enumeration.Interest.class)
public class InterestConverter implements Converter {
    private WebLocalization localization;
    private String preferredLocale;
    
    public InterestConverter() {
        /* LOCALIZATION */
        localization = new WebLocalization();
        /* END LOCALIZATION */
    }
    
    //getAsObject method converts a string to a an object
    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        
        preferredLocale = localization.getPreferredLocaleFromSession();
        
        
        if (value == null || value.length() == 0) {
            return null;
        }
        
        /* NOTE:
         * currently, the checkbox list actually returns all the same string for the object part
         * but, I just split them into diff locales just in case we need them one day.
         */
        
        // en_US ENGLISH, UNITED STATES
        if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_US))) {
            return getInterestAsObject_EN_US(value);
        }
        // en_SG ENGLISH, SINGAPORE
        else
        if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_SINGAPORE))) {
            return getInterestAsObject_EN_SG(value);
        }
        // ms_MY MALAY, MALAYSIA
        else
        if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.MALAY))) {
            return getInterestAsObject_MS_MY(value);
        }

        // zh_CN CHINESE, CHINA
        else
        if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.CHINESE_CHINA))) {
            return getInterestAsObject_ZH_CN(value);
        }
        // other language...
        else {
            return getInterestAsObject_EN_US(value);
        }
    }
    
    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        
        preferredLocale = localization.getPreferredLocaleFromSession();
        
        if (value == null) {
            return null;
        }
        
        if (value instanceof Interest) {
            Interest interest = (Interest) value;
            
            String localizedInterest = "";
            
            // en_US ENGLISH, UNITED STATES
            if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_US))) {
                localizedInterest = getInterestAsString_EN_US(interest);
            }
            // en_SG ENGLISH, SINGAPORE
            else
            if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_SINGAPORE))) {
                localizedInterest = getInterestAsString_EN_SG(interest);
            }
            // ms_MY MALAY, MALAYSIA
            else
            if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.MALAY))) {
                localizedInterest = getInterestAsString_MS_MY(interest);
            }

            // zh_CN CHINESE, CHINA
            else
            if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.CHINESE_CHINA))) {
                localizedInterest = getInterestAsString_ZH_CN(interest);
            }
            // other language...
            else {
                localizedInterest = getInterestAsString_EN_US(interest);
            }

            return localizedInterest;
        }
        else {
            throw new IllegalArgumentException("object " + value + " is of type " + value.getClass().getName() + "; expected type: util.enumeration.Interest");
        }
    }
    
    // simplified chinese (zh_CN)
    private Object getInterestAsObject_ZH_CN(String value) {
        if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.ANIMALS, SupportedLocale.getSupportedLocaleStringByLanguage(Language.CHINESE_CHINA)))) return Interest.ANIMALS;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.ARTS, SupportedLocale.getSupportedLocaleStringByLanguage(Language.CHINESE_CHINA)))) return Interest.ARTS;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.AUTOMOBILES, SupportedLocale.getSupportedLocaleStringByLanguage(Language.CHINESE_CHINA)))) return Interest.AUTOMOBILES;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.BEACH, SupportedLocale.getSupportedLocaleStringByLanguage(Language.CHINESE_CHINA)))) return Interest.BEACH;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.BEAUTY, SupportedLocale.getSupportedLocaleStringByLanguage(Language.CHINESE_CHINA)))) return Interest.BEAUTY;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.COMPUTERS, SupportedLocale.getSupportedLocaleStringByLanguage(Language.CHINESE_CHINA)))) return Interest.COMPUTERS;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.CULTURE, SupportedLocale.getSupportedLocaleStringByLanguage(Language.CHINESE_CHINA)))) return Interest.CULTURE;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.DANCING, SupportedLocale.getSupportedLocaleStringByLanguage(Language.CHINESE_CHINA)))) return Interest.DANCING;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.ELECTRONICS, SupportedLocale.getSupportedLocaleStringByLanguage(Language.CHINESE_CHINA)))) return Interest.ELECTRONICS;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.FISH, SupportedLocale.getSupportedLocaleStringByLanguage(Language.CHINESE_CHINA)))) return Interest.FISH;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.FISHING, SupportedLocale.getSupportedLocaleStringByLanguage(Language.CHINESE_CHINA)))) return Interest.FISHING;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.FOOD, SupportedLocale.getSupportedLocaleStringByLanguage(Language.CHINESE_CHINA)))) return Interest.FOOD;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.INDOOR_ACTIVITIES, SupportedLocale.getSupportedLocaleStringByLanguage(Language.CHINESE_CHINA)))) return Interest.INDOOR_ACTIVITIES;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.NATURE, SupportedLocale.getSupportedLocaleStringByLanguage(Language.CHINESE_CHINA)))) return Interest.NATURE;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.OUTDOOR_ACTIVITIES, SupportedLocale.getSupportedLocaleStringByLanguage(Language.CHINESE_CHINA)))) return Interest.OUTDOOR_ACTIVITIES;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.SPORTS, SupportedLocale.getSupportedLocaleStringByLanguage(Language.CHINESE_CHINA)))) return Interest.SPORTS;
//        if(value.equals("动物")) return Interest.ANIMALS;
//        else if(value.equals("艺术")) return Interest.ARTS;
//        else if(value.equals("汽车")) return Interest.AUTOMOBILES;
//        else if(value.equals("沙滩")) return Interest.BEACH;
//        else if(value.equals("美容")) return Interest.BEAUTY;
//        else if(value.equals("电脑")) return Interest.COMPUTERS;
//        else if(value.equals("文化")) return Interest.CULTURE;
//        else if(value.equals("舞蹈")) return Interest.DANCING;
//        else if(value.equals("电子")) return Interest.ELECTRONICS;
//        else if(value.equals("鱼")) return Interest.FISH;
//        else if(value.equals("钓鱼")) return Interest.FISHING;
//        else if(value.equals("食品")) return Interest.FOOD;
//        else if(value.equals("室内活动")) return Interest.INDOOR_ACTIVITIES;
//        else if(value.equals("自然")) return Interest.NATURE;
//        else if(value.equals("户外活动")) return Interest.OUTDOOR_ACTIVITIES;
//        else if(value.equals("体育")) return Interest.SPORTS;
        else return null;
    }
    
    // default (en_US)
    private Object getInterestAsObject_EN_US(String value) {
        if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.ANIMALS, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_US)))) return Interest.ANIMALS;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.ARTS, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_US)))) return Interest.ARTS;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.AUTOMOBILES, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_US)))) return Interest.AUTOMOBILES;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.BEACH, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_US)))) return Interest.BEACH;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.BEAUTY, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_US)))) return Interest.BEAUTY;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.COMPUTERS, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_US)))) return Interest.COMPUTERS;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.CULTURE, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_US)))) return Interest.CULTURE;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.DANCING, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_US)))) return Interest.DANCING;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.ELECTRONICS, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_US)))) return Interest.ELECTRONICS;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.FISH, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_US)))) return Interest.FISH;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.FISHING, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_US)))) return Interest.FISHING;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.FOOD, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_US)))) return Interest.FOOD;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.INDOOR_ACTIVITIES, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_US)))) return Interest.INDOOR_ACTIVITIES;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.NATURE, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_US)))) return Interest.NATURE;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.OUTDOOR_ACTIVITIES, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_US)))) return Interest.OUTDOOR_ACTIVITIES;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.SPORTS, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_US)))) return Interest.SPORTS;
        else return null;
    }
    
    // singapore english (en_SG)
    private static Object getInterestAsObject_EN_SG(String value) {
        if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.ANIMALS, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_SINGAPORE)))) return Interest.ANIMALS;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.ARTS, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_SINGAPORE)))) return Interest.ARTS;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.AUTOMOBILES, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_SINGAPORE)))) return Interest.AUTOMOBILES;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.BEACH, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_SINGAPORE)))) return Interest.BEACH;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.BEAUTY, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_SINGAPORE)))) return Interest.BEAUTY;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.COMPUTERS, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_SINGAPORE)))) return Interest.COMPUTERS;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.CULTURE, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_SINGAPORE)))) return Interest.CULTURE;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.DANCING, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_SINGAPORE)))) return Interest.DANCING;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.ELECTRONICS, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_SINGAPORE)))) return Interest.ELECTRONICS;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.FISH, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_SINGAPORE)))) return Interest.FISH;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.FISHING, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_SINGAPORE)))) return Interest.FISHING;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.FOOD, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_SINGAPORE)))) return Interest.FOOD;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.INDOOR_ACTIVITIES, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_SINGAPORE)))) return Interest.INDOOR_ACTIVITIES;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.NATURE, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_SINGAPORE)))) return Interest.NATURE;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.OUTDOOR_ACTIVITIES, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_SINGAPORE)))) return Interest.OUTDOOR_ACTIVITIES;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.SPORTS, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_SINGAPORE)))) return Interest.SPORTS;
        else return null;
    }
    
    // bahasa malaysia (ms_MY)
    private Object getInterestAsObject_MS_MY(String value) {
        if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.ANIMALS, SupportedLocale.getSupportedLocaleStringByLanguage(Language.MALAY)))) return Interest.ANIMALS;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.ARTS, SupportedLocale.getSupportedLocaleStringByLanguage(Language.MALAY)))) return Interest.ARTS;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.AUTOMOBILES, SupportedLocale.getSupportedLocaleStringByLanguage(Language.MALAY)))) return Interest.AUTOMOBILES;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.BEACH, SupportedLocale.getSupportedLocaleStringByLanguage(Language.MALAY)))) return Interest.BEACH;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.BEAUTY, SupportedLocale.getSupportedLocaleStringByLanguage(Language.MALAY)))) return Interest.BEAUTY;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.COMPUTERS, SupportedLocale.getSupportedLocaleStringByLanguage(Language.MALAY)))) return Interest.COMPUTERS;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.CULTURE, SupportedLocale.getSupportedLocaleStringByLanguage(Language.MALAY)))) return Interest.CULTURE;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.DANCING, SupportedLocale.getSupportedLocaleStringByLanguage(Language.MALAY)))) return Interest.DANCING;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.ELECTRONICS, SupportedLocale.getSupportedLocaleStringByLanguage(Language.MALAY)))) return Interest.ELECTRONICS;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.FISH, SupportedLocale.getSupportedLocaleStringByLanguage(Language.MALAY)))) return Interest.FISH;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.FISHING, SupportedLocale.getSupportedLocaleStringByLanguage(Language.MALAY)))) return Interest.FISHING;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.FOOD, SupportedLocale.getSupportedLocaleStringByLanguage(Language.MALAY)))) return Interest.FOOD;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.INDOOR_ACTIVITIES, SupportedLocale.getSupportedLocaleStringByLanguage(Language.MALAY)))) return Interest.INDOOR_ACTIVITIES;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.NATURE, SupportedLocale.getSupportedLocaleStringByLanguage(Language.MALAY)))) return Interest.NATURE;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.OUTDOOR_ACTIVITIES, SupportedLocale.getSupportedLocaleStringByLanguage(Language.MALAY)))) return Interest.OUTDOOR_ACTIVITIES;
        else if(value.equals(LocalizedInterest.getLocalizedInterestAsString(Interest.SPORTS, SupportedLocale.getSupportedLocaleStringByLanguage(Language.MALAY)))) return Interest.SPORTS;
        else return null;
    }
    
    // simplified chinese (zh_CN)
    private static String getInterestAsString_ZH_CN(Interest interest) {
        String localizedInterest = LocalizedInterest.getLocalizedInterestAsString(interest, SupportedLocale.getSupportedLocaleStringByLanguage(Language.CHINESE_CHINA));
        return localizedInterest;
    }
    
    // default (en_US)
    private static String getInterestAsString_EN_US(Interest interest) {
        String localizedInterest = LocalizedInterest.getLocalizedInterestAsString(interest, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_US));
        return localizedInterest;
    }
    
    // singapore english (en_SG)
    private static String getInterestAsString_EN_SG(Interest interest) {
        String localizedInterest = LocalizedInterest.getLocalizedInterestAsString(interest, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_SINGAPORE));
        return localizedInterest;
    }
    
    // bahasa malaysia (ms_MY)
    private static String getInterestAsString_MS_MY(Interest interest) {
        String localizedInterest = LocalizedInterest.getLocalizedInterestAsString(interest, SupportedLocale.getSupportedLocaleStringByLanguage(Language.MALAY));
        return localizedInterest;
    }
}